package com.nowcoder.code.lcof;

import java.util.ArrayList;
import java.util.PriorityQueue;

public class JZ40 {

    public static void main(String[] args) {
        System.out.println(new JZ40().GetLeastNumbers_Solution(new int[]{4, 5, 1, 6, 2, 7, 3, 8}, 0));
    }

    public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
        ArrayList list = new ArrayList(k+1);
        PriorityQueue<Integer> ik = new PriorityQueue<>(k+1, ((o1, o2) -> o2 - o1));

        for (int i : input) {
            ik.add(i);
            if (ik.size() == k + 1) {
                ik.poll();
            }
        }
        list.addAll(ik);
        return list;

    }

}
